package com.zhijian.warehouse.manage.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.BeanUtil;
import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.dao.ProductMapper;
import com.zhijian.warehouse.manage.entity.po.Product;
import com.zhijian.warehouse.manage.entity.vo.request.ProductSaveOrUpdateVo;
import com.zhijian.warehouse.manage.entity.vo.request.ProductSearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.ProductVo;
import com.zhijian.warehouse.manage.enums.BindEnum;
import com.zhijian.warehouse.manage.enums.DelFlagEnum;
import com.zhijian.warehouse.manage.enums.ProductEnum;
import com.zhijian.warehouse.manage.enums.StatusEnum;
import com.zhijian.warehouse.manage.service.ProductService;
import com.zhijian.warehouse.manage.util.AssertCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 产品业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-10-12 16:56:24
 */
@Slf4j
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper mapper;

    @Override
    public Product selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public ProductVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        Product product = this.selectById(id);
        AssertCommonUtil.notNull(product, "此数据不存在或者已被删除");
        AssertCommonUtil.check(Objects.equals(Integer.valueOf(product.getStatus()), StatusEnum.NORMAL.getCode()), "启用状态无法删除");
        this.updateByPrimaryKeySelective(Product.builder().id(id).isDelete(DelFlagEnum.DELETED.value).build());
        // mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            for (Long l : idList) {
                this.deleteById(l);
            }
        }
    }

    @Override
    public Long insert(Product product) {
        Date now = new Date();
        product.setCreateTime(now);
        product.setUpdateTime(now);
        return mapper.insert(product) > 0 ? product.getId() : 0L;
    }

    @Override
    public Long insertSelective(Product product) {
        Date now = new Date();
        product.setCreateTime(now);
        product.setUpdateTime(now);
        return mapper.insertSelective(product) > 0 ? product.getId() : 0L;
    }

    @Override
    public boolean update(Product product) {
        Date now = new Date();
        product.setUpdateTime(now);
        return mapper.updateByPrimaryKey(product) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(Product record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(ProductSaveOrUpdateVo saveOrUpdateVo) {
        Integer count = this.count(ProductSearchVo.builder()
                .number(saveOrUpdateVo.getNumber())
                .isDelete(DelFlagEnum.USING.value)
                .build());
        AssertCommonUtil.check(count > 0, "编号重复");
        Product product = new Product();
        BeanUtils.copyProperties(saveOrUpdateVo, product);
        product.setRfStatus(ProductEnum.CardStatusEnum.NO.getCode());
        if (BeanUtil.isNotEmpty(product.getRfId())) {
            product.setRfStatus(ProductEnum.CardStatusEnum.YES.getCode());
        }
        product.setCardStatus(Byte.valueOf(String.valueOf(product.getRfStatus())));
        product.setBarCodeStatus(BindEnum.BRA_CODE.UNBIND.value);
        if (BeanUtil.isNotEmpty(product.getBarCode())){
            product.setBarCodeStatus(BindEnum.BRA_CODE.BIND.value);
        }
        boolean b = this.insertSelective(product) != 0;
        saveOrUpdateVo.setId(product.getId());
        return b;
    }

    @Override
    public boolean modify(ProductSaveOrUpdateVo saveOrUpdateVo) {
        Product old = this.selectById(saveOrUpdateVo.getId());
        AssertCommonUtil.notNull(old, "此数据不存在或者已被删除");
        Integer count = this.count(ProductSearchVo.builder()
                .notId(saveOrUpdateVo.getId())
                .number(saveOrUpdateVo.getNumber())
                .isDelete(DelFlagEnum.USING.value)
                .build());
        AssertCommonUtil.check(count > 0, "编号重复");
        Product updateParam = new Product();
        BeanUtils.copyProperties(saveOrUpdateVo, updateParam, "createTime", "createUserId", "createUserName");
        updateParam.setIsDelete(DelFlagEnum.USING.value);
        updateParam.setRfStatus(ProductEnum.CardStatusEnum.NO.getCode());
        if (BeanUtil.isNotEmpty(updateParam.getRfId())) {
            updateParam.setRfStatus(ProductEnum.CardStatusEnum.YES.getCode());
        }
        updateParam.setCardStatus(Byte.valueOf(String.valueOf(updateParam.getRfStatus())));
        updateParam.setBarCodeStatus(BindEnum.BRA_CODE.UNBIND.value);
        if (BeanUtil.isNotEmpty(updateParam.getBarCode())){
            updateParam.setBarCodeStatus(BindEnum.BRA_CODE.BIND.value);
        }

        return this.update(updateParam);
    }

    @Override
    public ProductVo selectOne(ProductSearchVo productSearchVo) {
        return mapper.selectOne(productSearchVo);
    }

    @Override
    public Integer count(ProductSearchVo productSearchVo) {
        return mapper.count(productSearchVo);
    }

    @Override
    public List<ProductVo> selectList(ProductSearchVo productSearchVo) {
        return mapper.selectList(productSearchVo);
    }

    @Override
    public Page<ProductVo> selectPage(ProductSearchVo productSearchVo) {
        AssertCommonUtil.check(Objects.isNull(productSearchVo.getPageNum()) || Objects.isNull(productSearchVo.getPageSize()), "分页参数不能为空");
        if (BeanUtil.isNotEmpty(productSearchVo.getRfId())) {
            productSearchVo.setRfIdDim(productSearchVo.getRfId());
            productSearchVo.setRfId(null);
        }
        //先直接查总数
        int count = count(productSearchVo);
        //分页
        Page<ProductVo> page = new Page<>(true, count, productSearchVo.getPageNum(), productSearchVo.getPageSize());
        productSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<ProductVo> responseVos = this.selectList(productSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<Product> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public boolean bindRf(ProductSaveOrUpdateVo param) {
        AssertCommonUtil.notNull(param.getId(), "主键ID不能为空");
        AssertCommonUtil.notNull(param.getRfStatus(), "RFID操作状态不能为空");
        if (param.getRfStatus() == 1) {
            AssertCommonUtil.notNull(param.getRfId(), "RFID编码不能为空");
            Integer count = this.count(ProductSearchVo.builder()
                    .rfId(param.getRfId())
                    .isDelete(DelFlagEnum.USING.value)
                    .build());
            AssertCommonUtil.check(count > 0, "该RFID已被其他产品绑定");
            updateByPrimaryKeySelective(Product.builder().id(param.getId()).
                    rfId(param.getRfId()).
                    rfStatus(param.getRfStatus())
                    .cardStatus(Byte.valueOf(String.valueOf(param.getRfStatus())))
                    .build());

        } else {
            mapper.updateRfIdNull(param.getId());
            updateByPrimaryKeySelective(Product.builder().id(param.getId()).rfStatus(param.getRfStatus()).build());
        }
        return true;
    }

    @Override
    public boolean bindBarCode(ProductSaveOrUpdateVo param) {
        AssertCommonUtil.check(BeanUtil.isEmpty(param.getId()), "主键ID不能为空");
        AssertCommonUtil.check(BeanUtil.isEmpty(param.getBarCodeStatus()), "条码操作状态不能为空");
        if (Objects.equals(param.getBarCodeStatus(), BindEnum.RF_ID.BIND.value)) {
            AssertCommonUtil.check(BeanUtil.isEmpty(param.getBarCode()), "条码不能为空");
            Integer count = this.count(ProductSearchVo.builder()
                    .notId(param.getId())
                    .barCode(param.getBarCode())
                    .isDelete(DelFlagEnum.USING.value)
                    .build());
            AssertCommonUtil.check(count > 0, "该条码已被其他产品绑定");
            updateByPrimaryKeySelective(Product.builder().id(param.getId()).
                    barCode(param.getBarCode()).
                    barCodeStatus(BindEnum.RF_ID.BIND.value).build());
        } else {
            mapper.updateBarCodeNull(param.getId());
            updateByPrimaryKeySelective(Product.builder().id(param.getId()).barCodeStatus(BindEnum.RF_ID.UNBIND.value).build());
        }

        return true;
    }
}
